package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private PermissionDao permissionDao;

    //根据用户名查询数据库，获取用户信息和关联的角色以及权限信息
    public User findByUsername(String username) {
        //查询用户基本信息，不包含用户角色
        User user = userDao.findByUsername(username);
        if(user == null){
            return null;
        }

        Integer userId = user.getId();
        //根据用户id查询对应的角色
        Set<Role> roles = roleDao.findByUserId(userId);

        for (Role role : roles) {
            //根据角色id查询对应的权限
            Integer roleId = role.getId();
            Set<Permission> permissions = permissionDao.findByRoleId(roleId);
            role.setPermissions(permissions);//设置角色权限
        }

        user.setRoles(roles);//设置用户角色
        return user;
    }


    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {

        //当前页
        Integer currentPage = queryPageBean.getCurrentPage();
        //每页记录数
        Integer pageSize = queryPageBean.getPageSize();
        //分页条件
        String queryString = queryPageBean.getQueryString();

        //使用PageHelper完成分页
        PageHelper.startPage(currentPage, pageSize);

        Page<User> page = userDao.selectByCondition(queryString);
        //获取总记录数
        long total = page.getTotal();
        //获取数据集合
        List<User> rows = page.getResult();
       /* String s=null;
        for (User row : rows) {
            Set<Role> roles = row.getRoles();
            for (Role role : roles) {
                String name = role.getName();
                s=name;
                row.setRoles();
            }
        }*/
        return new PageResult(total, rows);
    }

    @Override
    public void add(User user) {
        userDao.add(user);
        Integer id = user.getId();
        Integer roleId = user.getRoleId();
        userDao.addroles(id,roleId);

    }

    @Override
    public User findById(Integer id) {
       return userDao.findById(id);
    }

    @Override
    public void edit(User user) {
        userDao.edit(user);
    }

    @Override
    public void deleteById(Integer id) {
        long count = userDao.findCountByCheckItemId(id);
        if(count > 0){
            //检查项关联检查组，不允许删除
            new RuntimeException();
        }
        userDao.deleteById(id);
    }

   /* @Override
    public void addroles(Integer id, Integer roleId) {
        userDao.addroles(id,roleId);
    }*/
}
